﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

/// <summary>
/// 设备工具类
/// 获取设备信息
/// </summary>
public class DeviceUtility
{
    /// <summary>
    /// 获取设备Id
    /// </summary>
    /// <returns></returns>
    public static string GetDeviceId()
    {
        string deviceId = "";
#if UNITY_ANDROID
        deviceId = GetAndroidDeviceId();
#elif UNITY_IPHONE
        deviceId = GetIosDeviceId();
#endif
        if (deviceId == SystemInfo.unsupportedIdentifier)
        {
            Debug.LogError("[DeviceUtility] can not get deviceId use guid instead");
            deviceId = GetGuid();
        }
        return deviceId;
    }

    private static string GetAndroidDeviceId()
    {
        string result = PlayerPrefs.GetString("deviceId", SystemInfo.deviceUniqueIdentifier);
        Debug.Log($"[DeviceUtility] GetAndroidDeviceId:{result}");
        return result;
    }

#if UNITY_IPHONE
    private static string GetIosDeviceId()
    {
        string result = PlayerPrefs.GetString("deviceId", _Get_Device_id());
        Debug.Log($"[DeviceUtility] GetIosDeviceId:{result}");
        return result;
    }

    [DllImport("__Internal")]
    static extern string _Get_Device_id();
#endif

    private static string GetGuid()
    {
        string result = System.Guid.NewGuid().ToString();
        Debug.Log($"[DeviceUtility] GetGuid: {result}");
        return result;
    } 
}
